<script setup lang="ts">
import {ShoppingCart, Menu as MenuIcon, Search, ArrowDown} from '@element-plus/icons-vue'
import { onMounted, ref, watch} from 'vue'
import {useRouter, useRoute} from 'vue-router'
import {fetchHeader} from "../request/req.ts";
import {useLogoTextStore} from "../stores/logoText.ts";

const useLogo = useLogoTextStore()
const router = useRouter()
const route = useRoute()
const drawerVisible = ref(false)
//分类数据模型
const categoryList = ref<any[]>([])

const openDrawer = () => {
  drawerVisible.value = true
}

const closeDrawer = () => {
  drawerVisible.value = false
}

const searcHK$eyword = ref('')
const searchType = ref('Sp')

// 搜索类型选项
const searchTypes = [
  {value: 'Sp', label: '商品'},
  {value: 'DD', label: '訂單'}
]

const goHome = () => {
  router.push('/')
}
const getHeadData = async () => {
  const res = await fetchHeader()
  categoryList.value = res.TypeList.map((category: any) => ({
    id: category.TypeId,
    name: category.TypeName
  }))
  logoText.value = res.ShopName
  useLogo.remove(2)
  useLogo.add({id: 2, text: res.ShopPhone})
  useLogo.remove(3)
  useLogo.add({id: 3, text: res.ServerUrl})
}
// 分类
const goClassification = (categoryId?: number) => {

  const category = categoryList.value.find(cat => cat.id === categoryId);
  const categoryName = category ? category.name : '';

  router.push({
    path: '/classification',
    query: {categoryId: categoryId,categoryName: categoryName }
  })
}

//搜索
const goSearch = () => {
  switch (searchType.value) {
    case 'Sp':
      router.push({
        path: '/search',
        query: {
          keyword: searcHK$eyword.value,
          type: searchType.value
        }
      })
      break
    case 'DD':
      router.push({
        path: '/dd',
        query: {
          keyword: searcHK$eyword.value,
          type: searchType.value
        }
      })
      break
  }
}

const logoText = ref('')

//购物车
const goCart = () => {
  router.push('/cart')
}


watch(() => route.fullPath, () => {
  drawerVisible.value = false
})

const isActive = (nameOrPath: string) => {
  return route.name === nameOrPath || route.path === nameOrPath
}
onMounted(() => {
  getHeadData()
})
</script>

<template>
  <div class="navbar">
    <!-- 左侧：移动端菜单按钮 -->
    <div class="left-group">
      <button class="menu-toggle" @click="openDrawer" aria-label="切换菜单">
        <el-icon :size="24" color="dodgerblue">
          <MenuIcon/>
        </el-icon>
      </button>

      <div class="logo">
        <div class="logo-text" @click="goHome">
          <span class="logo-text">{{ logoText }}</span>
        </div>
      </div>
    </div>

    <!-- 桌面端导航 -->
    <nav class="nav-links desktop-nav">
      <span class="nav-link dropdown-toggle" :class="{ active: isActive('Home') }" @click="goHome">首頁</span>
      <div class="dropdown">
        <el-dropdown @command="goClassification" trigger="click"
                     :max-height="400"
                     popper-class="category-dropdown-popper"
                     size="large"
                     placement="bottom-start">
          <span class="nav-link dropdown-toggle" :class="{ active: isActive('Classification') }">
            商品分類
            <el-icon class="dropdown-arrow">
              <ArrowDown />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu class="category-dropdown-menu">
              <el-dropdown-item
                v-for="category in categoryList"
                :key="category.id"
                :command="category.id"
                class="category-dropdown-item"
              >
                {{ category.name }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </nav>

    <!-- 移动端Drawer抽屉 -->
    <el-drawer
      v-model="drawerVisible"
      direction="ltr"
      size="280px"
      :with-header="false"
      :before-close="closeDrawer"
      class="mobile-drawer"
    >
      <div class="drawer-content">
        <div class="drawer-nav-item" @click="goHome">
          <span class="drawer-nav-text">首頁</span>
        </div>

        <div class="drawer-category-section">
          <div class="drawer-section-title">商品分類</div>
          <div
            v-for="category in categoryList"
            :key="category.id"
            class="drawer-category-item"
            @click="goClassification(category.id)"
          >
            {{ category.name }}
          </div>
        </div>
      </div>
    </el-drawer>

    <div class="cart-icon">
      <div class="search-box">
        <el-icon class="search-icon">
          <Search/>
        </el-icon>
        <input type="text" placeholder="搜索商品/訂單" class="search-input" v-model="searcHK$eyword"
               @keyup.enter="goSearch()">
        <div class="search-type-selector">
          <el-dropdown v-model="searchType">
            <span class="el-dropdown-link">
              {{ searchTypes.find(type => type.value === searchType)?.label || '商品' }}
              <el-icon class="el-icon--right">
                <arrow-down/>
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item
                    v-for="type in searchTypes"
                    :key="type.value"
                    @click="searchType = type.value"
                >
                  {{ type.label }}
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <button class="search-btn" @click="goSearch()">搜索</button>
      </div>
      <el-button link @click="goCart()">
        <el-icon :size="30" color="dodgerblue" >
          <ShoppingCart/>
        </el-icon>
      </el-button>
    </div>
  </div>
</template>

<style scoped>
.navbar {
  display: flex;
  justify-content: space-between;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
  align-items: center;
  position: relative;
  background-color: #1a252f;
  z-index: 1001;
  padding: 0 40px;
  height: 86px;
}

.logo-text {
  font-size: 30px;
  color: #d1d1d1;
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 300px;
  transition: all 0.3s ease;
}

.left-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.menu-toggle {
  display: none;
  border: none;
  background: transparent;
  padding: 6px;
  border-radius: 6px;
  cursor: pointer;
}

.menu-toggle:hover {
  background: #f5f5f5;
}

.logo {
  display: flex;
  align-items: center;
}

.nav-links {
  display: flex;
  align-items: center;
  gap: 30px;
  flex-direction: row;
}

.nav-link {
  text-decoration: none;
  color: rgba(242, 242, 242, 0.44);
  font-weight: bold;
  font-size: 22px;
  transition: color 0.3s;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.nav-link:hover,
.nav-link.active {
  color: white;
}

.dropdown {
  position: relative;
}

.dropdown-toggle {
  cursor: pointer;
}

.dropdown-arrow {
  margin-left: 5px;
}

.category-dropdown-menu {
  min-width: 200px !important;
  max-width: 280px !important;
  padding: 10px 0 !important;
  border-radius: 10px !important;
  box-shadow: 0 6px 25px rgba(0, 0, 0, 0.2) !important;
  border: 1px solid #e4e7ed !important;
}

.nav-link,
.dropdown-toggle {
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 8px;
  transition: background-color .2s ease, color .2s ease, transform .05s ease;
}

.nav-link:hover,
.dropdown-toggle:hover {
  background-color: rgba(76, 175, 80, 0.08);
}

.nav-link:active,
.dropdown-toggle:active {
  background-color: rgba(209, 209, 209, 0.16);
  transform: translateY(1px);
}

:global(.el-drawer) {
  background-color: #1a252f !important;
}


.drawer-nav-item {
  padding: 16px 20px;
  border-bottom: 1px solid #4a5f73;
  cursor: pointer;
  transition: all 0.2s ease;
}


.drawer-nav-text {
  color: #d1d1d1;
  font-size: 16px;
  font-weight: 500;
}

.drawer-nav-item:hover .drawer-nav-text {
  color: white;
}

.drawer-category-section {
  padding: 20px 0;
}

.drawer-section-title {
  padding: 0 20px 12px;
  color: #bdc3c7;
  font-size: 14px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.drawer-category-item {
  padding: 12px 20px;
  color: #d1d1d1;
  font-size: 15px;
  cursor: pointer;
  transition: all 0.2s ease;
  border-bottom: 1px solid #2c3e50;
}

.drawer-category-item:hover {
  color: white;
}

.search-box {
  margin-right: 20px;
  position: relative;
  display: flex;
  align-items: center;
  background: white;
  border-radius: 18px;
  border: 1px solid dodgerblue;
  overflow: hidden;
}

.search-icon {
  position: absolute;
  left: 15px;
  color: #999;
  font-size: 14px;
  z-index: 1;
}

.search-input {
  background-color: #1a252f;
  flex: 1;
  padding: 0 150px 0 34px;
  border: none;
  outline: none;
  font-size: 13px;
  color: #d1d1d1;
  height: 100%;
  line-height: 1;
}

.search-input::placeholder {
  color: #999;
}

.search-type-selector {
  position: absolute;
  right: 68px;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
}

.search-type-selector :deep(.el-dropdown-link:focus) {
  border: none;
  box-shadow: none;
  outline: none;
}

.search-type-selector :deep(.el-dropdown-link) {
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(245, 245, 245, 0.73);
  height: 100%;
  padding: 0;
  border: none;
  box-shadow: none;
  outline: none;
  font-size: 13px;
  background: transparent;
  cursor: pointer;
  transition: all 0.2s ease;
}

.custom-select option {
  background: white;
  color: #333;
  padding: 8px 12px;
}

.select-arrow svg {
  transition: transform 0.2s ease;
}

.custom-select:focus + .select-arrow svg {
  transform: rotate(180deg);
}

.search-btn {
  position: absolute;
  right: 0;
  background: dodgerblue;
  color: white;
  border: none;
  padding: 0 15px;
  border-radius: 0 18px 18px 0;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background-color 0.3s;
  min-width: 68px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-btn:hover {
  background: dodgerblue;
}

/* 购物车图标样式 */
.cart-icon {
  display: flex;
  position: relative;
}

.cart-icon a {
  text-decoration: none;
  color: #333;
  font-size: 22px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  transition: background-color 0.3s;
}

.cart-icon a:hover {
  background-color: #f5f5f5;
}


@media (max-width: 1024px) {
}

@media (max-width: 768px) {
  .navbar {
    height: 60px;
    width: 100%;
  }

  .menu-toggle {
    display: inline-flex;
  }

  .logo {
    display: none;
  }

  .desktop-nav {
    display: none;
  }

  .search-box {
    margin-right: 10px;
    width: 200px;
  }

  .search-input {
    padding: 0 120px 0 30px;
    font-size: 12px;
  }
  .search-input::placeholder {
    color: transparent;
  }

  .search-type-selector {
    right: 50px;
  }

  .search-btn {
    padding: 0 12px;
    min-width: 50px;
    font-size: 12px;
  }
}

@media (max-width: 480px) {
  .navbar {
    padding: 0 10px;
  }

  .search-box {
    width: 160px;
    margin-right: 8px;
  }

  .search-input {
    padding: 0 100px 0 25px;
    font-size: 11px;
  }
  .search-input::placeholder {
    color: transparent;
  }

  .search-type-selector {
    right: 45px;
  }

  .search-btn {
    min-width: 45px;
    font-size: 11px;
    padding: 0 8px;
  }
}
</style>
